package com.xiangxiangjd.controller;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.xiangxiangjd.bean.TrUser;
import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.utils.StringUtilsXiao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.service.HandoverDateNoService;
import com.xiangxiangjd.bean.HandoverDateNo;
import com.xiangxiangjd.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申领航材批次号 前端控制器
 *
 * @author 肖守凯
 * @since 2021-11-08
 */
@Controller
@RequestMapping("/api/handoverDateNo")
@Api(tags = "交接航材订单")
public class HandoverDateNoController extends BaseController {

  /**
   * 添加HandoverDateNo
   *
   * @param handoverDateNo 前端传进的参数
   * @return 返回统一信息
   */
  @PostMapping(value = "/addHandoverDateNo")
  @ResponseBody
  @ApiOperation(value = "交接航材", notes = "交接航材")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ApiResultModel saveHandoverDateNo(@RequestBody HandoverDateNo handoverDateNo) {
    System.err.println(handoverDateNo);

    String handoverUsersId = handoverDateNo.getHandoverUsersId();


    TrUser user = trUserService.getById(handoverUsersId);
    System.out.println("***********************************:");
    System.out.println(user);

    if(user==null){
      return $.success("交接人已不存在");
    }

    handoverDateNo.setHandoverUnitId(user.getUnitId());
    return handoverDateNoService.addHandoverDateNo(handoverDateNo);
  }

  /**
   * 分页查询HandoverDateNo
   *
   * @param page 页码
   * @param limit 查几条
   * @param handoverDateNo 前端传进的参数
   * @return 返回统一分页信息
   */
  @PostMapping(value = "admin/findHandoverDateNoByPage")
  @ResponseBody
  @ApiOperation(value = "查询交接单号", notes = "查询交接单号")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResultPage findHandoverDateNoByPage(
      Integer page, Integer limit, HandoverDateNo handoverDateNo) {


    return handoverDateNoService.findHandoverDateNoByPage(page, limit, handoverDateNo);
  }

  /**
   * 编辑HandoverDateNo
   *
   * @param handoverDateNo 前端传进的参数
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/editHandoverDateNo")
  @ResponseBody
  public ApiResultModel editHandoverDateNo(HandoverDateNo handoverDateNo) {
    return handoverDateNoService.editHandoverDateNo(handoverDateNo);
  }

  /**
   * 删除HandoverDateNo
   *
   * @param ids 前端传进的ID
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/delHandoverDateNo")
  @ResponseBody
  public ApiResultModel delHandoverDateNo(@RequestParam("datas") ArrayList ids) {

    return handoverDateNoService.delHandoverDateNos(ids);
  }

  /** 交接审批 */
  @PostMapping(value = "/handoverApproval")
  @ResponseBody
  @ApiOperation(value = "交接审批", notes = "交接审批")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ApiResultModel handoverApproval(
      @RequestParam(value = "claimDateNoId") Integer claimDateNoId,
      @RequestParam(value = "claimdateNoState") String claimdateNoState,String reasonForFfailure) {


    return handoverDateNoService.handoverApproval(claimDateNoId, claimdateNoState,reasonForFfailure,getUserInfo().getId());
  }

  /** 查询订单详情 */
  @PostMapping(value = "/getByHandover")
  @ResponseBody
  @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ApiResultModel getByHandover(@RequestParam(value = "handoverDateNoId") Integer handoverDateNoId) {

    return handoverDateNoService.getByHandover(handoverDateNoId);
  }


  /**
   * 处置报告表
   */
  @GetMapping(value = "/improt")
  @ApiOperation(value = "交接 word 导出", notes = "交接 word 导出")
 // @PreAuthorize("hasRole('ROLE_USER')")
  public void downDocHandoverDateNo(@RequestParam("handoverDateNoId") Integer handoverDateNoId) throws Exception {

    //提交

    Map map=handoverDateNoService.downDocHandoverDateNo(handoverDateNoId);




    XWPFDocument doc = WordExportUtil.exportWord07("word/HandoverDocx.docx", map);

    doc.enforceReadonlyProtection();

    String fileName = System.currentTimeMillis() + ".docx";
    HttpServletResponse response = getResponse();

    response.reset();
    response.setContentType("application/msword");
    String dispositionValue = "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8");
    response.setHeader("Content-disposition", dispositionValue);
    response.setCharacterEncoding("UTF-8");

    OutputStream output = response.getOutputStream();
    doc.write(output);
    output.close();

  }


}
